//
//  selectView.m
//  EasyToTesco
//
//  Created by ym on 16/3/28.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "selectView.h"

@implementation selectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArray = [NSMutableArray arrayWithObjects:@"现金", @"积分", @"活动", nil];
        self.selectedColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1];
        self.unselectedColor = [UIColor colorWithRed:181/255. green:180/255. blue:180/255. alpha:1];
        self.userInteractionEnabled = YES;
        [self addSubViewUI];
    }
    return self;
}


- (void)addSubViewUI {
    CGFloat weight = CGRectGetWidth(self.frame)/self.titleArray.count;
    CGFloat height = CGRectGetHeight(self.frame);
    for (NSInteger i = 0; i < self.titleArray.count; i++) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.frame = CGRectMake(weight * i, 0, weight, height);
        titleButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        titleButton.tag = 1000+i;
        titleButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [titleButton setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [titleButton addTarget:self action:@selector(selectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleButton setTitleColor:self.unselectedColor forState:UIControlStateNormal];
        if (i==0) {
            [titleButton setTitleColor:self.selectedColor forState:UIControlStateNormal];
        }
        [self addSubview:titleButton];
    }
}

- (void)selectButtonClick:(UIButton *)button {
    for (UIButton *tempBtn in self.subviews) {
        if (tempBtn.tag == button.tag) {
            [tempBtn setTitleColor:self.selectedColor forState:UIControlStateNormal];
        }else{
            [tempBtn setTitleColor:self.unselectedColor forState:UIControlStateNormal];
        }
    }
    [self.delegate selectBtnClick:button];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
